package com.gxa.learning.portal.controller;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Course;
import com.gxa.learning.core.pojo.Feedback;

import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.portal.mapper.ProjectMapper;
import com.gxa.learning.portal.service.CourseService;
import com.gxa.learning.portal.service.StudyPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/13 20:28
 */
@RestController
@RequestMapping("/studyPlan")
@Api(tags = "学习培训")
public class StudyPlanController {

    @Autowired
    private StudyPlanService studyPlanService;

    @Autowired
    private CourseService courseService;


    @GetMapping("/projectList")
    @ApiOperation("参加的培训列表")
    public Map<String, Object> projectList(String userId) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> userProjectList = studyPlanService.getUserProjectList(userId);

        jsonResult.setCode("1");
        jsonResult.setData(userProjectList);
        jsonResult.put("total", userProjectList.size());
        return jsonResult.getDataMap();
    }

    @GetMapping("/courseList")
    @ApiOperation("参加的课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "培训项目编号", name = "projectId"),
            @ApiImplicitParam(value = "用户编号", name = "userId")
    })
    public Map<String, Object> courseList(String projectId, String userId) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> courseList = studyPlanService.getCourseList(projectId, userId);
        jsonResult.setCode("1");
        jsonResult.put("total", courseList.size());
        jsonResult.setData(courseList);
        return jsonResult.getDataMap();
    }

    @GetMapping("/signInList")
    @ApiOperation("参签到列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "培训项目id", dataType = "string", required = true),
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "string", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程编号", dataType = "string", required = true),
    })
    public Map<String, Object> signIn(String projectId, String userId, String courseId) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> signInList = studyPlanService.getSignInList(userId, projectId, courseId);
        jsonResult.setCode("1");
        jsonResult.setData(signInList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/course")
    @ApiOperation("单个课程")
    @ApiImplicitParam(name = "course", value = "course", dataType = "course", paramType = "body", required = true)
    public Map<String, Object> study(@RequestBody Course course) throws Exception {
        System.out.println("请求进来了");
        if (course == null || course.getId() == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        System.out.println(course.getId());
        System.out.println(course);
        JsonResult jsonResult = new JsonResult();
        Course course1 = courseService.selectById(course.getId());
        jsonResult.setCode("1");
        jsonResult.setData(course1);
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "学员签到")
    @GetMapping("/signIn")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "pid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "uid", value = "uid", required = true, dataType = "Long", paramType = "query", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    public Map<String, Object> signIn(Long pid, Long uid, String key) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        studyPlanService.signIn(pid, uid, key);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/insertFeedback")
    @ApiOperation("提交项目反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedback", value = "feedback的Json对象 {\"userId\": 9,\"projectId\":1}", dataType = "json")
    })
    public Map<String, Object> insertFeedback(@RequestBody Feedback feedback) throws Exception {

        Integer courseId = feedback.getCourseId();
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> byUserAndProject = new HashMap<>(2);
        if (courseId == null) {
            byUserAndProject = studyPlanService.findByUserAndProject(feedback.getUserId(), feedback.getProjectId(), null);
        } else {
            byUserAndProject = studyPlanService.findByUserAndProject(feedback.getUserId(), null, feedback.getCourseId());

        }

        Long id = Long.valueOf(byUserAndProject.get("id").toString());
        feedback.setState(1);
        System.out.println(feedback.getState());
        feedback.setId(id);
        studyPlanService.updateById(feedback);
        System.out.println(feedback);
        jsonResult.setCode("1");
        jsonResult.setMsg("提交成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/projectOne")
    @ApiOperation("单个项目")
    @ApiImplicitParam(name = "id", value = "项目id")
    public Map<String, Object> projectOne(String id) throws BusinessException {
        System.out.println(id);
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> byId = studyPlanService.getById(id);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(byId);
        return jsonResult.getDataMap();
    }

    @GetMapping("/courseNum")
    @ApiOperation("课程评分")
    @ApiImplicitParam(name = "courseId", value = "课程id")
    public Map<String, Object> courseNum(String courseId) {
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> byId = studyPlanService.courseNum(courseId);
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(byId);
        return jsonResult.getDataMap();
    }

    @GetMapping("/courseFeed")
    @ApiOperation("课程反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedback", value = "feedback的Json对象 {\"userId\": 9,\"projectId\":1}", dataType = "json")
    })
    public Map<String, Object> courseFeed(@RequestBody Feedback feedback) {
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("成功");

        return jsonResult.getDataMap();
    }
}
